﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace KimPhongCRM.Lib
{
    public static class stringUtils
    {
        public static string RandomID()
        {
            string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
            string randomstring = "";
            for (var i = 0; i < 32; i++)
            {
                int rnum = Convert.ToInt32(Math.Round(Math.Floor(new Random().NextDouble() * (chars.Length - 1)), 0));
                randomstring += chars.Substring(rnum, 1);
            }
            return randomstring;
        }
        public static String ParseButDontClip(string original, int maxLength)
        {
            string response = original;
            int count = 8;
            if (original.Length - maxLength < 8)
                count = original.Length - maxLength;
            if (original.Length > maxLength)
            {

                int lastSpace = original.IndexOf(' ', maxLength, count);
                if (lastSpace > -1)
                    response = original.Substring(0, lastSpace);
                else
                    response = original.Substring(0, 150);
            }
            return response;
        }
        public static String trimStr(string original, int maxLength)
        {
            string response = original;
            int l = original.Length;
            if(l>maxLength)
                response = original.Substring(0,maxLength)+"...";
            else
                response = original.Substring(0, l) + ".";
            return response;
        }
        public static String CreateAlias(string orginal)
        {
            if (!string.IsNullOrEmpty(orginal))
            {
                Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
                string strFormD = orginal.Normalize(System.Text.NormalizationForm.FormD);
                orginal = regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
                orginal = Regex.Replace(orginal, @"([<>\?\*\\\""/\|\!\@\#\$\%\^\&\(\)\:])+", "-");
                orginal = Regex.Replace(orginal, @"\s+", "-");
                orginal = Regex.Replace(orginal, @"[-]+", "-");
                return orginal.ToLower();
            }
            else
            {
                return "";
            }
        }

        public static string EncodeTo64(string toEncode)
        {

            byte[] toEncodeAsBytes

                  = System.Text.Encoding.Unicode.GetBytes(toEncode);

            string returnValue

                  = System.Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }
        public static string DecodeFrom64(string encodedData)
        {

            byte[] encodedDataAsBytes

                = System.Convert.FromBase64String(encodedData);

            string returnValue =

               System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);

            return returnValue;

        }
    }

}